package com.apress.springrecipes.shop;

import java.io.File;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.PriorityOrdered;

public class PathCheckingBeanPostProcessor implements BeanPostProcessor,
		PriorityOrdered {
	private int order;

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		if (bean instanceof StorageConfig) {
			String path = ((StorageConfig) bean).getPath();
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
		}

		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		return bean;
	}

	public int setOrder(int order) {
		return this.order = order;
	}

	public int getOrder() {
		return order;
	}

}
